package com.accloud.service;

import com.accloud.cloudservice.AC;
import com.accloud.common.ACConfiguration;
import com.accloud.utils.PreferencesUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ACMsg extends ACObject {
    public static final String ACK_MSG = "x-zc-ack";
    public static final String ERR_MSG = "x-zc-err";
    public static final String JSON_PAYLOAD = "text/json";
    public static final String MSG_NAME_HEADER = "x-zc-msg-name";
    public static final String OBJECT_PAYLOAD = "application/x-zc-object";
    public static final String STREAM_PAYLOAD = "application/octet-stream";
    private ACContext context;
    private String name;
    private byte[] payload;
    private String payloadFormat;
    private int payloadSize;
    private InputStream streamPayload;

    public String getName() {
        return this.name;
    }

    public void setName(String name2) {
        this.name = name2;
    }

    public String getPayloadFormat() {
        return this.payloadFormat;
    }

    public byte[] getPayload() {
        return this.payload;
    }

    public int getPayloadSize() {
        return this.payloadSize;
    }

    public void setPayload(byte[] payload2, String format) {
        this.payloadFormat = format;
        this.payload = payload2;
        this.payloadSize = payload2.length;
    }

    public InputStream getStreamPayload() {
        if (this.payload == null) {
            return this.streamPayload;
        }
        return new ByteArrayInputStream(this.payload);
    }

    public void closeStreamPayload() throws IOException {
        this.streamPayload.close();
    }

    public void setStreamPayload(InputStream payload2, int size) {
        if (size < 0) {
            throw new IllegalArgumentException("size must be larger than 0");
        }
        this.payloadFormat = "application/octet-stream";
        this.streamPayload = payload2;
        this.payloadSize = size;
    }

    public boolean isErr() {
        return !this.name.toLowerCase().equals(ACK_MSG);
    }

    public Integer getErrCode() {
        return Integer.valueOf(((Number) get("errorCode")).intValue());
    }

    public String getErrMsg() {
        return (String) get("error");
    }

    public String getErrDesc() {
        return (String) get("description");
    }

    public ACContext getContext() {
        return this.context;
    }

    public void setContext(ACContext context2) {
        this.context = context2;
    }

    public void setContext(String subDomain) {
        setContext(new ACContext(AC.majorDomain, subDomain, PreferencesUtils.getLong(AC.context, ACConfiguration.KEY_USERID)));
    }

    public static ACMsg getACMsg(String subDomain) {
        ACContext context2 = new ACContext(AC.majorDomain, subDomain, PreferencesUtils.getLong(AC.context, ACConfiguration.KEY_USERID, 0));
        ACMsg msg = new ACMsg();
        msg.setContext(context2);
        return msg;
    }
}
